package com.nuoniu.sibanyun.controller.production;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpProcessRoute;
import com.nuoniu.sibanyun.entity.erp.ErpProcessRouteDetail;
import com.nuoniu.sibanyun.entity.vo.ErpProcessRouteDetailVo;
import com.nuoniu.sibanyun.entity.vo.ProcessRouteAndPriceDetailVo;
import com.nuoniu.sibanyun.service.erp.IErpProcessRouteDetailService;
import com.nuoniu.sibanyun.service.erp.IErpProcessRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 工艺路线 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-30
 */
@RestController
@RequestMapping("/erp/processRoute")
@Api(value = "工艺路线管理", tags = "工艺路线管理")
public class ErpProcessRouteController extends BaseController {

    @Autowired
    private IErpProcessRouteService processRouteService;
    @Autowired
    private IErpProcessRouteDetailService erpProcessRouteDetailService;

    @GetMapping("getList")
    @ApiOperation("获取列表")
    public HttpResult<ErpProcessRoute> getList(Page page, ErpProcessRoute processRoute) {
        return HttpResult.ok(processRouteService.getList(page, processRoute));
    }

    @ApiOperation("获取工艺明细")
    @GetMapping("getDetailList")
    public HttpResult<ErpProcessRouteDetail> getDetailList(Integer id) {
        return HttpResult.ok(processRouteService.getDetailList(id));
    }


    @ApiOperation("获取工艺_工序明细")
    @GetMapping("listErpProcessRouteDetailVo")
    public HttpResult<ErpProcessRouteDetailVo> listErpProcessRouteDetailVo(Integer id) {
        return HttpResult.ok(processRouteService.listErpProcessRouteDetailVo(id));
    }

    @PostMapping("save")
    @ApiOperation("保存工艺")
    public HttpResult save(@RequestBody ErpProcessRoute processRoute) {
        if(processRoute == null || processRoute.getProcessRouteDetails().size() == 0) {
            throw new NuoNiuException("工艺明细不能为空");
        }

        if(StringUtils.isBlank(processRoute.getCode())) {
            throw new NuoNiuException("编码不能为空");
        }

        if(StringUtils.isBlank(processRoute.getName())) {
            throw new NuoNiuException("名称不能为空");
        }

        processRouteService.saveProcessRoute(processRoute);
        return HttpResult.ok();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除")
    public HttpResult delete(Long id) {
        processRouteService.delete(id);
        return HttpResult.ok();
    }


    @GetMapping("examine")
    @ApiOperation("工艺审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工艺id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        processRouteService.examine(id,status);
        return HttpResult.ok();
    }

    @GetMapping("listProcessRouteAndPriceDetailVo")
    @ApiOperation("通过工艺ID 查询 工艺明细 以及 对应最新的工价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工艺id")
    })
    public HttpResult<ProcessRouteAndPriceDetailVo> listProcessRouteAndPriceDetailVo(Integer id) {
        return HttpResult.ok(erpProcessRouteDetailService.listProcessRouteAndPriceDetailVo(id));
    }

    @PostMapping("listProcessRouteAndPriceDetailVoByRouteDetailIds")
    @ApiOperation("通过工艺明细ID 查询 工艺明细 以及 对应最新的工价")
    public HttpResult<ProcessRouteAndPriceDetailVo> listProcessRouteAndPriceDetailVoByRouteDetailIds(@RequestBody List<Integer> id) {
        return HttpResult.ok(erpProcessRouteDetailService.listProcessRouteAndPriceDetailVoByRouteDetailIds(id));
    }



}
